<?php

function utf8_strlen($string = null) {
// 将字符串分解为单元
    preg_match_all("/./us", $string, $match);
// 返回单元个数
    return count($match[0]);
}

function hideStar($str) { //用户名、邮箱、手机账号中间字符串以*隐藏 
    if (strpos($str, '@')) {
        $email_array = explode("@", $str);
        $prevfix = (strlen($email_array[0]) < 4) ? "" : substr($str, 0, 3); //邮箱前缀 
        $count = 0;
        $str = preg_replace('/([\d\w+_-]{0,100})@/', '***@', $str, -1, $count);
        $rs = $prevfix . $str;
    } else {
        $pattern = '/(1[3458]{1}[0-9])[0-9]{4}([0-9]{4})/i';
        if (preg_match($pattern, $str)) {
            $rs = preg_replace($pattern, '$1****$2', $str); // substr_replace($name,'****',3,4); 
        } else {
            $rs = substr($str, 0, 3) . "***" . substr($str, -1);
        }
    }
    return $rs;
}

function getRefundState($fuhao) {
    if ($fuhao == 0) {
        $word = "<b class='red'>支出</b>";
    } else {
        $word = "<b class='green'>收入</b>";
    }
    return $word;
}

function getPointsState($type) {
    if ($type == 0) {
        $word = "<span style='color:#333'>待审核</span>";
    } elseif ($type == 1) {
        $word = "<span style='color:#FF6600'>已完成</span>";
    } else {
        $word = "<span style='color:#FF0000'>审核失败</span>";
    }
    return $word;
}

function addPoints($mtype, $money, $uid, $title, $status, $fuhao) {
    $data['addtime'] = time();
    $data['uid'] = $uid ? $uid : session("userid");
    $data['mtype'] = $mtype;
    $data['money'] = $money;
    $data['title'] = $title;
    $data['status'] = $status ? $status : 0;
    $data['fuhao'] = $fuhao != '' ? $fuhao : 0;
    $lastid = M("points")->add($data);
    if ($lastid > 0) {
        if ($fuhao == 1) {
            M('user')->where("id=" . $data['uid'] . "")->setInc('money', $data['money']); //setInc加
        } elseif ($fuhao == -2) { //点券不打入账号中
        } else {
            M('user')->where("id=" . $data['uid'] . "")->setDec('money', $data['money']); //setInc加
        }
    }
    return $lastid;
}

//公共函数
function msubstr($str, $start = 0, $length, $charset = "utf-8", $suffix = true) {
    if (function_exists("mb_substr"))
        $slice = mb_substr($str, $start, $length, $charset);
    elseif (function_exists('iconv_substr')) {
        $slice = iconv_substr($str, $start, $length, $charset);
        if (false === $slice) {
            $slice = '';
        }
    } else {
        $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
        $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
        $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
        $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
        preg_match_all($re[$charset], $str, $match);
        $slice = join("", array_slice($match[0], $start, $length));
    }
    $fix = '';
    if (strlen($slice) < strlen($str)) {
        $fix = '...';
    }
    return $suffix ? $slice . $fix : $slice;
}

function getUserAvatar($avatar) {
    $logo_rs = "Public/images/avatar.jpg";
    if ($avatar) {
        $logo_rs = $avatar;
    }
    return $logo_rs;
}

function getUserAvatarId($uid) {
    $user_info = M("user")->field("avatar")->where("id = " . $uid . "")->find();
    return getUserAvatar($user_info['avatar']);
}

function threeLogin() {
    $arr = array(
        0 => array(
            "code" => "qq",
            "logo" => "qq"
        ),
        1 => array(
            "code" => "renren",
            "logo" => "qq"
        ),
        2 => array(
            "code" => "sina",
            "logo" => "qq"
        ),
    );
    return $arr;
}

function getChecked($ids, $id) {
    if (!empty($ids)) {
        $ids = explode(",", $ids);
        if (in_array($id, $ids)) {
            return "checked";
        }
    }
}

function getSingleField($id, $table, $field, $word) {
    if ($id > 0) {
        $info = M($table)->field($field)->where("id = " . $id . "")->find();
        if ($info[$field] != '') {
            return htmlspecialchars_decode($info[$field]);
        } else {
            return $word;
        }
    }
}

function getMoreField($id, $table, $fields) {
    if ($id > 0) {
        $info = M($table)->field($fields)->where("id = " . $id . "")->find();
        return $info;
    }
}

function getIdsField($ids, $table, $field = 'name') {
    if ($ids) {
        $names = M($table)->field($field)->where("id in (" . $ids . ")")->select();
        foreach ($names as $v) {
            $arr[] = $v[$field];
        }
        return implode("、", $arr);
    }
}

function getEqual($a, $b, $rs, $fuhao = 'eq') {
    if ($fuhao > 0) {
        if ($a % $fuhao == $b) {
            return $rs;
        }
    } else {
        if ($fuhao == 'eq') {
            if ($a == $b) {
                return $rs;
            }
        } elseif ($fuhao == 'gt') {
            if ($a > $b) {
                return $rs;
            }
        } elseif ($fuhao == 'lt') {
            if ($a < $b) {
                return $rs;
            }
        } elseif ($fuhao == 'in') {
            $arr = explode(",", $b);
            if (in_array($a, $arr)) {
                return $rs;
            }
        } elseif ($fuhao == 'neq') {

            if ($a != $b) {
                return $rs;
            }
        }
    }
}

function getEqualTwo($a, $b, $rs, $rs2) {
    $result = $rs;
    if ($a != $b) {
        $result = $rs2;
    }
    return $result;
}

function getStar($star) {
    $span = "<span class='c-value-no c-value-4d" . intval($star) . "'><em></em></span>";
    return $span;
}

function getThreeField($three, $big, $small) {
    $detail = json_decode($three[$big], true);
    return $detail[$small];
}

function getTableFile($table) {
    $commonTable = array("accounts", "friends");
    $ordTable = array("dictionary", "modals_tags");
    if (in_array($table, $commonTable)) {
        $ord = "ord ASC";
        $where = "is_check=1";
    } elseif (in_array($table, $ordTable)) {
        $ord = "ord ASC,id DESC";
    }
    $lists = F('' . $table . '/data');
    if (empty($lists)) {
        $lists = M($table)->where($where)->order($ord)->select();

        F('' . $table . '/data', $lists);
    }
    return $lists;
}

function getTableConfig() {
    $config = M("config")->where("id = 1")->find();
    return $config;
}

function getGb2312($file) {
    return iconv('UTF-8', 'GB2312', $file);
}

function getUtf8($file) {
    return iconv('GB2312', 'UTF-8', $file);
}

function getUrlJson($url, $type = 0) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 获取数据返回  
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回  
    $rs = curl_exec($ch);
    if ($type == 1) {
        $rs = json_decode($rs, true);
    }
    return $rs;
}

function getHtmlCacheRule($paras) {
    $parasArr = explode(",", $paras);
    $id = $parasArr[0];
    $para = $parasArr[1];
    $mtype = $parasArr[2];
    if ($id > 0) {
        $html = $mtype . "/" . getFileBei($id) . $id;
    } else if ($id == '' && $para == '') {
        $html = $mtype . "/index";
    } else {

        $html = $mtype . "/paras/" . $para;
    }
    return $html;
}

function getCommentContent($content) {
    $content = stripslashes($content);
    $content = str_replace("[code]", "<pre><code class='html'>", $content);
    $content = str_replace("[code='html']", "<pre><code class='html'>", $content);
    $content = str_replace("[code='js']", "<pre><code class='js'>", $content);
    $content = str_replace("[code='css']", "<pre><code class='css'>", $content);
    $content = str_replace("[code='php']", "<pre><code class='php'>", $content);
    $content = str_replace("[/code]", "</code></pre>", $content);
    $tagsArr = array("code", "pre", "em", "b", "strong", "p", "br", "a", "span", "dd", "dl", "dt", "table", "tr", "th", "td", "thead", "tbody", "img",
        "h1", "h2", "h3", "h4", "h5", "h5", "div", "i", "dfn", "u", "ins", "strike", "s", "del", "font", "hr", "center", "caption", "html", "body", "ul", "li", "ol", "select", "small", "input", "textarea"
    );
    $tags = "";
    foreach ($tagsArr as $v) {
        $tags .="<" . $v . ">";
    }
    return strip_tags($content, $tags);
}

function getLogo($logo, $width, $height, $name) {
    $fileArr = pathinfo($logo);
    $dirname = $fileArr['dirname'];
    $filename = $fileArr['filename'];
    $extension = $fileArr['extension'];
    $logo_rs = "";
    if ($width > 0 && $height > 0) {
        $name_thumb = $dirname . "/" . $filename . "_" . $width . "_" . $height . "." . $extension;
        if (!file_exists($name_thumb)) {
            if (file_exists($logo)) {
                $image = new \Think\Image();
                $image->open($logo);
                $image->thumb($width, $height, \Think\Image::IMAGE_THUMB_FIXED)->save($name_thumb);
            } else {
                $name_thumb = "";
            }
        }
        if ($name_thumb) {
            $logo_rs = $name_thumb;
        }
    } else if ($width > 0 && $height == 0) {
        $image_info = getimagesize($logo);
        $width_real = $image_info[0];
        $height_real = $image_info[1];
        $height = $width * ($height_real / $width_real);
        $name_thumb = $dirname . "/" . $filename . "_" . $width . "_0" . "." . $extension;
        if (!file_exists($name_thumb)) {
            if (file_exists($logo)) {
                $image = new \Think\Image();
                $image->open($logo);
                $image->thumb($width, $height)->save($name_thumb);
            } else {
                $name_thumb = "";
            }
        }
        if ($name_thumb) {
            $logo_rs = $name_thumb;
        }
    } else {
        $logo_rs = $logo;
    }
    if ($logo_rs) {

        if ($name == 1) {
            return $logo_rs;
        } else {
            if ($name) {
                return "<img src='" . __APP__ . "/" . $logo_rs . "' alt='" . $name . "'/>";
            } else {
                return __APP__ . "/" . $logo_rs;
            }
        }
    }
}

function delPics($pic, $pics) {
    $type = substr(strrchr($pic, '.'), 1);
    $pic_pre = substr($pic, 0, -(strlen($type) + 1));
    foreach ($pics as $v) {
        $name = $pic_pre . "_" . $v . "." . $type;
        if (file_exists($name)) {
            unlink($name);
        }
    }
    if (file_exists($pic)) {
        unlink($pic);
    }
}

function getIntval($num) {
    return intval($num);
}

function getIP() {
    if (!empty($_SERVER["HTTP_CLIENT_IP"])) {
        $cip = $_SERVER["HTTP_CLIENT_IP"];
    } elseif (!empty($_SERVER["HTTP_X_FORWARDED_FOR"])) {
        $cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
    } elseif (!empty($_SERVER["REMOTE_ADDR"])) {
        $cip = $_SERVER["REMOTE_ADDR"];
    } else {
        $cip = "无法获取！";
    }
    return $cip;
}

function getDot($num = 2) {
    return sprintf("%.2f", $num);
}

function getTableField($field, $table) {
    $rs = F($field);
    if (empty($rs)) {
        $info = M($table)->field($field)->where("id = 1")->find();
        $rs = $info[$field];
    }
    return $rs;
}

function roundPercent($num) {
    if ($num == 0) {
        return 0;
    } else {
        return sprintf("%.2f", $num * 100);
    }
}

function getDecimalZero($num) { //保留两位小数，若是0 则为0
    if ($num == 0) {
        $num = "0";
    } else {
        $num = sprintf("%.2f", $num);
    }
    return $num;
}

function getSessionCookie($k) {
    $s_k = session($k);
    $rs = $s_k ? $s_k : cookie($k);
    return $rs;
}

function getTwoChoseSingle($a, $b) {
    return $a ? $a : $b;
}

function setSessionCookie($k, $v) {
    session($k, $v);
    cookie($k, $v);
}

function getUserId() {
    $userid = session("userid");
    $username = session("username");
    if (!$userid || !$username) {
        $c_checkcode = getSessionCookie("unionCheckcode");
        if ($c_checkcode) {
            $userid = cookie('userid');
            if ($c_checkcode == getUnionLoginCheckcode($userid)) {
                $info = M("user")->field("name")->where("id= '" . $userid . "'")->find();
                if (!empty($info['name'])) {
                    session('username', $info['name']);
                    session('userid', $userid);
                }
            }
        } else {
            emptySessionCookie('userid');
            session('admin_uid', null);
        }
    } else {
        $c_checkcode = getSessionCookie("unionCheckcode");
//        echo $c_checkcode."<hr>".getUnionLoginCheckcode($userid);
        if ($c_checkcode != getUnionLoginCheckcode($userid)) {
            $userid = "";
        }
    }
    return $userid;
}

function getAdminId() {
    $userid = session("admin_uid");
    $username = session("admin_name");
    if (!$userid || !$username) {
        $c_checkcode = getSessionCookie("unionCheckcode");
        if ($c_checkcode) {
            $userid = cookie('admin_uid');
            if ($c_checkcode == getUnionLoginCheckcode($userid)) {
                $info = M("user")->field("name")->where("id= '" . $userid . "' AND is_admin = 1")->find();
                if (!empty($info['name'])) {
                    session('admin_name', $info['name']);
                    session('admin_uid', $userid);
                }
            }
        } else {
            emptySessionCookie('userid');
            emptySessionCookie('admin_uid');
        }
    } else {
        $c_checkcode = getSessionCookie("unionCheckcode");
//        echo $c_checkcode."<hr>".getUnionLoginCheckcode($userid);
        if ($c_checkcode != getUnionLoginCheckcode($userid)) {
            $userid = "";
        }
    }
    return $userid;
}

function getUnionLoginCheckcode($uid) {
    $privateKey = C("privateKey");
    return md5($uid . $privateKey);
}

function getYearPercent($year) {
    $config = M("config")->field("rent_percent")->where("id = 1")->find();
    $percents = explode(",", $config['rent_percent']);
    return $percents[($year - 1)];
}

function emptySessionCookie($k) {
    session($k, null);
    cookie($k, null);
}

function getSex($sex) {
    $rs = "保密";
    if ($sex == 1) {
        $rs = "先生";
    } elseif ($sex == 2) {
        $rs = "女士";
    }
    return $rs;
}

function getUserMoney($money) {
    if ($money > 0) {
        $money = "<span class='red'>" . $money . "</span>";
    }
    return $money;
}

function getUsername($uid) {
    $userinfo = M("user")->field("name")->where("id = '" . $uid . "'")->find();
    return $userinfo['name'];
}

function getOrdernum() {
    $year_code = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
    return $year_code[intval(date('Y')) - 2010] .
            strtoupper(dechex(date('m'))) . date('d') .
            substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('d', rand(0, 99));
}

function getUpdateResult($rs) {
    $arr = array(
        "code" => 0,
        "result" => "ok",
    );
    if ($rs == false) {
        $arr = array(
            "code" => "system_error",
            "result" => "系统错误，请稍后再试！",
        );
    }
    return $arr;
}

function removeEightSix($phone) {
    $pattern = "/^([\.a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/";
    if (!preg_match($pattern, $phone)) {
        return preg_replace('/^86(\d+)/', '${1}', $phone);
    } else {
        return $phone;
    }
}

function getHttpUrl($url) {
    if (!preg_match('/http:\/\/[\w.]+[\w\/]*[\w.]*\??[\w=&\+\%]*/is', $url)) {
        $url = __APP__ . "/" . $url;
    }
    return $url;
}

function merge_pics($pics, $id) {
    if (count($pics) < 2) {
        return $pics;
    }

    $height_max = 0;
    $pics_new = array();
    foreach ($pics as $k => $v) {
        $img_info = getimagesize($v);
        $widths[] = $img_info[0];
        $height_max += $img_info[1];
        $pics_new[$k]['pic'] = $v;
        $pics_new[$k]['width'] = $img_info[0];
        $pics_new[$k]['height'] = $img_info[1];
    }

    $width_max = max($widths);
    $merge_img = imagecreatetruecolor($width_max, $height_max);
    $trans_colour = imagecolorallocatealpha($merge_img, 255, 255, 255, 127);
    imagefill($merge_img, 0, 0, $trans_colour);
    $height = 0;

    foreach ($pics_new as $k => $v) {
        $fileArr = pathinfo($v['pic']);
        $extension = $fileArr['extension'];
        if ($k == 0) {
            if ($extension == 'png') {
                // imagecopyresized($merge_img, imagecreatefrompng($v['pic']), 0, 0, 0, 0, $v['width'], $v['height'], $v['width'], $v['height']);
            } else {
//                echo $v['width']."***".$v['height'];
//                $v['width'] =280;
//                $v['height'] = 83;
                imagecopyresized($merge_img, imagecreatefromjpeg($v['pic']), 0, 0, 0, 0, $v['width'], $v['height'], $v['width'], $v['height']);
            }
        } else {
            if ($extension == 'png') {
                // imagecopyresized($merge_img, imagecreatefrompng($v['pic']), 0, $height, 0, 0, $v['width'], $height, $v['width'], $height);
            } else {
                imagecopyresized($merge_img, imagecreatefromjpeg($v['pic']), 0, $height, 0, 0, $v['width'], $height, $v['width'], $height);
            }
        }
        $height += $v['height'];
    }
    $pic_heti = 'uploads/heti/' . $id . '.jpg';
    imagejpeg($merge_img, $pic_heti);
    return $pic_heti;
}

//function merge_pics($pics,$id) {
//    if(count($pics)<=1){
//        return $pics[0];
//    }
//    $height_max = 0;
//    $pics_new = array();
//    foreach ($pics as $k => $v) {
//        $img_info = getimagesize($v);
//        $widths[] = $img_info[0];
//        $height_max += $img_info[1];
//        $pics_new[$k]['pic'] = $v;
//        $pics_new[$k]['width'] = $img_info[0];
//        $pics_new[$k]['height'] = $img_info[1];
//    }
//    $width_max = max($widths);
//    $merge_img = imagecreatetruecolor($width_max, $height_max);
//    $trans_colour = imagecolorallocatealpha($merge_img, 255, 255, 255, 127);
//    imagefill($merge_img, 0, 0, $trans_colour);
//    $height = 0;
////    print_r($pics_new);exit;
//    foreach ($pics_new as $k => $v) {
//        $fileArr = pathinfo($v['pic']);
//        $extension = $fileArr['extension'];
//        if ($k == 0) {
//            if ($extension == 'png') {
//                imagecopyresized($merge_img, imagecreatefrompng($v['pic']), 0, 0, 0, 0, $v['width'], $v['height'], $v['width'], $v['height']);
//            } else {
//                imagecopyresized($merge_img, imagecreatefromjpeg($v['pic']), 0, 0, 0, 0, $v['width'], $v['height'], $v['width'], $v['height']);
//            }
//        } else {
//            if ($extension == 'png') {
//                imagecopyresized($merge_img, imagecreatefrompng($v['pic']), 0, $height, 0, 0, $v['width'], $height, $v['width'], $height);
//            } else {
//                imagecopyresized($merge_img, imagecreatefromjpeg($v['pic']), 0, $height, 0, 0, $v['width'], $height, $v['width'], $height);
//            }
//        }
//        $height += $v['height'];
//    }
//    $pic_heti = "uploads/heiti/".$id.".jpg";
//    imagejpeg($merge_img, $pic_heti);
//
//    return $pic_heti;
//}


function sysSortArray($ArrayData, $KeyName1, $SortOrder1 = "SORT_ASC", $SortType1 = "SORT_REGULAR") {
    if (!is_array($ArrayData)) {
        return $ArrayData;
    }
    $ArgCount = func_num_args();
    for ($I = 1; $I < $ArgCount; $I ++) {
        $Arg = func_get_arg($I);
        if (!eregi("SORT", $Arg)) {
            $KeyNameList[] = $Arg;
            $SortRule[] = '$' . $Arg;
        } else {
            $SortRule[] = $Arg;
        }
    }
    foreach ($ArrayData AS $Key => $Info) {
        foreach ($KeyNameList AS $KeyName) {
            ${$KeyName}[$Key] = $Info[$KeyName];
        }
    }
    $EvalString = 'array_multisort(' . join(",", $SortRule) . ',$ArrayData);';
    eval($EvalString);
    return $ArrayData;
}

function getLogoWeixin($logo, $width, $height, $name) {
    $fileArr = pathinfo($logo);
    $dirname = $fileArr['dirname'];
    $filename = $fileArr['filename'];
    $extension = $fileArr['extension'];

    $logo_rs = "";
    if ($width > 0 && $height > 0) {
        $name_thumb = $dirname . "/" . $filename . "_" . $width . "_" . $height . "." . $extension;

        if (file_exists($logo)) {
            $image = new \Think\Image();
            $image->open($logo);
            $image->thumb($width, $height, \Think\Image::IMAGE_THUMB_FIXED)->save($name_thumb);
        } else {
            $name_thumb = "";
        }

        if ($name_thumb) {
            $logo_rs = $name_thumb;
        }
    } else if ($width > 0 && $height == 0) {
        $image_info = getimagesize($logo);
        $width_real = $image_info[0];
        $height_real = $image_info[1];
        $height = $width * ($height_real / $width_real);
        $name_thumb = $dirname . "/" . $filename . "_" . $width . "_0" . "." . $extension;
        $logo_path = parse_url($logo, PHP_URL_PATH);
//      $logo_path = "1.jpg";
        if (file_exists($logo_path)) {
            $image = new \Think\Image();
            $image->open($logo_path);
            $image->thumb($width, $height)->save($name_thumb);
        } else {
            $name_thumb = "";
        }

        if ($name_thumb) {
            $logo_rs = $name_thumb;
        }
    } else {
        $logo_rs = $logo;
    }
    if ($logo_rs) {

        if ($name == 1) {
            return $logo_rs;
        } else {
            if ($name) {
                return "<img src='" . __APP__ . "/" . $logo_rs . "' alt='" . $name . "'/>";
            } else {
                return __APP__ . "/" . $logo_rs;
            }
        }
    }
}
function curl_post($url, $data) {
    $ch = curl_init();
    $headers[] = "Accept-Charset: utf-8";
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
   curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
//        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));  
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);
    $result = deformatNVP($result);
    //关闭curl
    curl_close($ch);

    return $result;
}
//function curl_post($url, $data) {
//    $ch = curl_init();
//    curl_setopt($ch, CURLOPT_URL, $url);
//    curl_setopt($ch, CURLOPT_VERBOSE, 1);
//
//    //turning off the server and peer verification(TrustManager Concept).
//    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
//    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
//    //curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);//curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
//    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//    curl_setopt($ch, CURLOPT_POST, 1);
//
//    //if USE_PROXY constant set to TRUE in Constants.php, then only proxy will be enabled.
//    //Set proxy name to PROXY_HOST and port number to PROXY_PORT in constants.php 
//    //NVPRequest for submitting to server
//    //$nvpreq="METHOD=" . urlencode($methodName) . "&VERSION=" . urlencode($version) . "&PWD=" . urlencode($API_Password) . "&USER=" . urlencode($API_UserName) . "&SIGNATURE=" . urlencode($API_Signature) . $nvpStr . "&BUTTONSOURCE=" . urlencode($sBNCode);
//    $nvpreq = http_build_query($data);
//    //setting the nvpreq as POST FIELD to curl
//    curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpreq);
//
//    //getting response from server
//    $response = curl_exec($ch);
//
//    //convrting NVPResponse to an Associative Array
//    $nvpResArray = deformatNVP($response);
////    $nvpReqArray = deformatNVP($nvpreq);
////    $_SESSION['nvpReqArray'] = $nvpReqArray;
//
//    if (curl_errno($ch)) {
//        print_r(curl_errno($ch));
//        print_r(curl_error($ch));
//    } else {
//        //closing the curl
//        curl_close($ch);
//    }
//
//    return $nvpResArray;
//}

function deformatNVP($nvpstr) {
    $intial = 0;
    $nvpArray = array();

    while (strlen($nvpstr)) {
        //postion of Key
        $keypos = strpos($nvpstr, '=');
        //position of value
        $valuepos = strpos($nvpstr, '&') ? strpos($nvpstr, '&') : strlen($nvpstr);

        /* getting the Key and Value values and storing in a Associative Array */
        $keyval = substr($nvpstr, $intial, $keypos);
        $valval = substr($nvpstr, $keypos + 1, $valuepos - $keypos - 1);
        //decoding the respose
        $nvpArray[urldecode($keyval)] = urldecode($valval);
        $nvpstr = substr($nvpstr, $valuepos + 1, strlen($nvpstr));
    }
    return $nvpArray;
}

?>
